// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var GameDefine = require("GameDefine");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        _animation : {
            default : null,
            type : cc.Animation,
        },

        _rotation : 0.0, //旋转（逆时针）
        _velocity : new cc.Vec2(0.0, 0.0), //移动速度
        _operateDirection : new cc.Vec2(1, 0), //操作方向
        _operateType : GameDefine.operateType.NULL,  //加速类型
        _isStop : true, //是否停止

        canvas : {
            default : null,
            type : cc.Node,
        },

        backGround :{
            default : null,
            type : cc.Node,
        },

        maxNormalVelocity : {
            default : 1000.0,
            tooltip : "慢走最大速度",
        },
        maxFastVelocity : {
            default : 3000,
            tooltip : "快走最大速度",
        },
        normalAcce : {
            default : 37.5,
            tooltip : "慢走加速度",
        },
        fastAcce : {
            default : 62.5,
            tooltip : "快走加速度",
        },
        slowDownAcce : {
            default : 1.25,
            tooltip : "减速加速度",
        },
        rotationSpeed : {
            default : 200,
            tooltip : "旋转角速度",
        },
        // hitForce : {
        //     default : 2000,
        //     tooltip : "打击力"
        // },
    },

    // onLoad () {
        
    // },

    start () {
        this._animation = this.node.getComponent(cc.Animation);
    },

    setOperateType(operateType){
        this._operateType = operateType;
    },

    setOperateDirection(direction){
        this._operateDirection = direction;
    },

    hit(){
        // var angle = this._rotation * Math.PI / 180;
        // var faceVec = new cc.Vec2(1, 0).rotateSelf(angle);
        // this._hitNegForce = faceVec.normalize().neg().mul(this.hitForce / this.mass);
    },

    update (dt) {
        //更新朝向动画
        if(this._rotation > 45 && this._rotation <= 135){
            this.setAnimation("Player_Up");
        }else if(this._rotation > 135 && this._rotation <= 225){
            this.setAnimation("Player_Left");
        }else if(this._rotation > 225 && this._rotation <= 315){
            this.setAnimation("Player_Down");
        }else{
            this.setAnimation("Player_Right");
        }
        //减速
        if(this._operateType == GameDefine.operateType.NULL) {
            if(!this._isStop){
                if(this._velocity.mag() == 0){
                    this._isStop = true;
                    return;
                }
                //摩擦力
                var slowDownAcceVec = this._velocity.normalize().neg().mul(this.slowDownAcce);
                
                //更新位置
                var offset = this._velocity.mul(dt).add(slowDownAcceVec.mul(0.5 * Math.pow(dt, 2)));
                this.changePlayerPos(offset);
                //更新当前速度
                this._velocity.addSelf(slowDownAcceVec.mul(dt));
                //判断停止状态
                var neg = this._velocity.neg().normalize();
                if(this._velocity.mag() == 0 || neg == this._velocity.normalize()){
                    this._isStop = true;
                }
            }
        }else{
            //当前朝向弧度
            var angle = this._rotation * Math.PI / 180;
            var faceVec = new cc.Vec2(1, 0).rotateSelf(angle);
            var isOperate = this._operateDirection.mag() != 0;
            if(isOperate){
                var signAngle = this._operateDirection.signAngle(faceVec);
                //角度偏差
                var isCorrectRotation = Math.abs(signAngle) <= (2 * Math.PI / 180);
                if(!isCorrectRotation){
                    if((signAngle > 0 && signAngle <= Math.PI) || (signAngle < 0 && signAngle <= -Math.PI)) {
                        this._rotation += this.rotationSpeed * dt;
                        if(this._rotation >= 360.0){
                            this._rotation -= 360.0;
                        }
                    }else{
                        this._rotation -= this.rotationSpeed * dt;
                        if(this._rotation < 0.0){
                            this._rotation += 360.0;
                        }
                    }
                }
            }

            if(this._operateType == GameDefine.operateType.NORMAL){ //普通行走
                if(!isCorrectRotation){
                    //摩擦力
                    var slowDownAcceVec = this._velocity.normalize().neg().mul(this.slowDownAcce);
                    //更新位置
                    var offset = this._velocity.mul(dt).add(slowDownAcceVec.mul(0.5 * Math.pow(dt, 2)));
                    this.changePlayerPos(offset);
                    //更新当前速度
                    this._velocity.addSelf(slowDownAcceVec.mul(dt));
                }else{
                    this._isStop = false;
                    //合摩擦力加速度
                    var slowDownAcceVec = this._velocity.normalize().neg().mul(this.slowDownAcce);
                    //合控制力加速度
                    var operateAcceVec = this._operateDirection.normalize().mul(this.normalAcce);
                    //合加速度
                    operateAcceVec.addSelf(slowDownAcceVec);
                    //更新位置
                    var offset = this._velocity.mul(dt).add(operateAcceVec.mul(0.5 * Math.pow(dt, 2)));
                    this.changePlayerPos(offset);
                    //更新当前速度
                    this._velocity.addSelf(operateAcceVec.mul(dt));
                    if(this._velocity.mag() > this.maxNormalVelocity){
                        this._velocity.normalizeSelf().mulSelf(this.maxNormalVelocity);
                    }
                }
            }else{
                this._isStop = false;
                //合摩擦力加速度
                var slowDownAcceVec = this._velocity.normalize().neg().mul(this.slowDownAcce);
                //合控制力加速度
                var operateAcceVec = faceVec.normalize().mul(this.fastAcce);
                //合加速度
                operateAcceVec.addSelf(slowDownAcceVec);
                //更新位置
                var offset = this._velocity.mul(dt).add(operateAcceVec.mul(0.5 * Math.pow(dt, 2)));
                this.changePlayerPos(offset);
                //更新当前速度
                this._velocity.addSelf(operateAcceVec.mul(dt));
                if(this._velocity.mag() > this.maxFastVelocity){
                    this._velocity.normalizeSelf().mulSelf(this.maxFastVelocity);
                }
            }
        }
    },

    changePlayerPos(_offset){
        var canvasW = this.canvas.width;
        var canvasH = this.canvas.height;
        var bgW = this.backGround.width;
        var bgH = this.backGround.height;

        var moveOffset = new cc.Vec2(0.0, 0.0);
        if(_offset.x > 0 && this.node.x < 0){
            if((_offset.x + this.node.x) > 0){
                moveOffset.x = -this.node.x;
            }else{
                moveOffset.x = _offset.x;
            }
        }else if(_offset.x < 0 && this.node.x > 0){
            if((_offset.x + this.node.x) < 0){
                moveOffset.x = -this.node.x;
            }else{
                moveOffset.x = _offset.x;
            }
        }
        this.node.x +=  moveOffset.x;
        _offset.x -=  moveOffset.x;

        if(_offset.y > 0 && this.node.y < 0){
            if((_offset.y + this.node.y) > 0){
                moveOffset.y = -this.node.y;
            }else{
                moveOffset.y = _offset.y;
            }
        }else if(_offset.y < 0 && this.node.y > 0){
            if((_offset.y + this.node.y) < 0){
                moveOffset.y = -this.node.y;
            }else{
                moveOffset.y = _offset.y;
            }
        }
        this.node.y += moveOffset.y;
        _offset.y -= moveOffset.y;

        var left = (canvasW - bgW) * 0.5;
        var right = -left;
        var bottom = (canvasH - bgH) * 0.5;
        var top = -bottom;
        
        if((this.backGround.x - _offset.x) <= left){
            moveOffset.x = left - this.backGround.x;
        }else if((this.backGround.x - _offset.x) >= right){
            moveOffset.x = right - this.backGround.x;
        }else{
            moveOffset.x = -_offset.x;
        }

        this.backGround.x += moveOffset.x;
        _offset.x += moveOffset.x;

        if((this.backGround.y - _offset.y) <= bottom){
            moveOffset.y = bottom - this.backGround.y;
        }else if((this.backGround.y - _offset.y) >= top){
            moveOffset.y = top - this.backGround.y;
        }else{
            moveOffset.y = -_offset.y;
        }
        this.backGround.y += moveOffset.y;
        _offset.y += moveOffset.y;

        if((this.node.x + _offset.x) > ((canvasW - this.node.width) * 0.5)){
            this.node.x = (canvasW - this.node.width) * 0.5;
            this._velocity.x = 0;
        }else if((this.node.x + _offset.x) < ((-canvasW + this.node.width) * 0.5)){
            this.node.x = (-canvasW + this.node.width) * 0.5;
            this._velocity.x = 0;
        }else{
            this.node.x += _offset.x;
        }

        if((this.node.y + _offset.y) > ((canvasH - this.node.height) * 0.5)){
            this.node.y = (canvasH - this.node.height) * 0.5;
            this._velocity.y = 0;
        }else if((this.node.y + _offset.y) < ((-canvasH + this.node.height) * 0.5)){
            this.node.y = (-canvasH + this.node.height) * 0.5;
            this._velocity.y = 0;
        }else{
            this.node.y += _offset.y;
        }
    },
 
    setAnimation(clipName){
        var currentClip = this._animation.currentClip;
        if(currentClip){
            if(currentClip.name == clipName){
                return;
            }    
        }
        this._animation.play(clipName);
    }
});
